package com.xxx.blog.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;

import com.xxx.base.vo.ArticleTypeVo;
import com.xxx.base.vo.GdVo;
import com.xxx.blog.domain.ArticleType;
import com.xxx.blog.query.ArticleTypeQuery;
import com.xxx.blog.service.IArticleTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articleType")
@Api(tags = "articleType接口")
public class ArticleTypeController {
    @Autowired
    IArticleTypeService articleTypeService;

    @GetMapping
    @ApiOperation(value = "分页所有")
    public R<List<ArticleType>> findAll(){
        List<ArticleType> articleTypes = articleTypeService.findAll();
        return R.ok(articleTypes);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public R<ArticleType> findOne(@PathVariable long id){
        ArticleType one = articleTypeService.findOne(id);
        return R.ok(one);
    }

    @PostMapping
    @ApiOperation(value = "分页加条件查询")
    public PageInfo<ArticleType> queryPage(@RequestBody ArticleTypeQuery articleTypeQuery){
        List<ArticleType> list = articleTypeService.queryPage(articleTypeQuery);
        return new PageInfo<ArticleType>(list);
    }

    @PutMapping
    @ApiOperation(value = "查询新增")
    public R<Void> updateArticleType(@RequestBody ArticleType articleType){
        Long id = articleType.getId();
        try {
            if(id==null){
                articleTypeService.addArticleType(articleType);
            }else {
            articleTypeService.updateArticleType(articleType);}
        } catch (Exception e) {
            e.printStackTrace();
           return R.fail();
        }
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除某个")
    public R<Void> deleteArticleType(@PathVariable long id){
        try {
            articleTypeService.deleteArticleType(id);
        } catch (Exception e) {
            e.printStackTrace();
            R.fail();
        }
        return R.ok();
    }

    @PatchMapping
    @ApiOperation(value = "批量删除")
    public R<Void> batch(@RequestBody Long[] ids){
        try {
            articleTypeService.batchArticleType(ids);
        } catch (Exception e) {
            e.printStackTrace();
            R.fail();
        }
        return R.ok();
    }


    @GetMapping("/getTypeTree")
    @ApiOperation(value = "查询类型树")
    public R<List<ArticleType>> getTypeTree(){
        List<ArticleType> articleTypes = articleTypeService.getTypeTree();
        return R.ok(articleTypes);
    }

    @GetMapping("/articleTypeStatistics")
    @ApiOperation(value = "分类查询")
    public List<ArticleTypeVo> getArticleTypeStatistics(){
        List<ArticleTypeVo> res = articleTypeService.getArticleTypeStatistics();
        return res;
    }


}
